package cn.jkgly.suggest.controller;

import java.util.Date;


import cn.jkgly.log.entity.WxUserLog;
import cn.jkgly.log.service.IWxUserLogService;
import cn.jkgly.suggest.entity.WxUserSuggest;
import cn.jkgly.suggest.service.IWxUserSuggestService;
import cn.jkgly.wxUser.entity.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Description: wx_user_suggest
 * @Author: jeecg-boot
 * @Date: 2020-02-09
 * @Version: V1.0
 */
@RestController
@Slf4j
public class WxUserSuggestController {
    @Autowired
    private IWxUserSuggestService wxUserSuggestService;

    @Autowired
    IWxUserLogService wxUserLogService;

    /**
     * 提交建议
     *
     * @param _suggest
     * @return
     */
    @PostMapping(value = "/PostGuggest")
    @ResponseBody//这个注解意味着这个方法的返回值不是页面名称，而是同一次请求的返回体（也就是服务器返回给客户端的数据）
    public Result<?> PostGuggest(@RequestBody WxUserSuggest _suggest) {
        QueryWrapper<WxUserSuggest> queryWrapper = new QueryWrapper<WxUserSuggest>();
        queryWrapper.eq("openid", _suggest.getOpenid());
        WxUserSuggest suggest = wxUserSuggestService.getOne(queryWrapper);
        if (suggest != null) {
            suggest.setUpdatetime(new Date());
            suggest.setSuggest(_suggest.getSuggest());
            wxUserSuggestService.saveOrUpdate(suggest);
            log(_suggest.getOpenid(), "更新建议:" + _suggest.getSuggest());
            System.out.println(suggest.getSuggest());
        } else {
            _suggest.setCreatetime(new Date());
            _suggest.setUpdatetime(new Date());
            log(_suggest.getOpenid(), "新增建议:" + _suggest.getSuggest());
            wxUserSuggestService.save(_suggest);
        }
        return Result.ok(suggest);
    }

    /**
     * 获取建议
     *
     * @param openid
     * @return
     */
    @GetMapping(value = "/GetGuggest")
    @ResponseBody//这个注解意味着这个方法的返回值不是页面名称，而是同一次请求的返回体（也就是服务器返回给客户端的数据）
    public Result<?> GetGuggest(@RequestParam(name = "openid", required = true) String openid) {
        QueryWrapper<WxUserSuggest> queryWrapper = new QueryWrapper<WxUserSuggest>();
        queryWrapper.eq("openid", openid);
        WxUserSuggest suggest = wxUserSuggestService.getOne(queryWrapper);
        if (suggest != null) {
            System.out.println(suggest.getSuggest());
            log(suggest.getOpenid(), "获取建议" + suggest.getSuggest());
            return Result.ok(suggest);
        } else {
            log(openid, "未找到建议记录");
            return Result.error("未找到记录");
        }
    }

    public void log(String _openid, String _even) {
        WxUserLog log = new WxUserLog();
        log.setCreatetime(new Date());
        log.setOpenid(_openid);
        log.setEventinfo(_even);
        wxUserLogService.save(log);
    }
}
